(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.profSubAcctEditController', profSubAcctEditController);
	profSubAcctEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig'];
	function profSubAcctEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("profSubAcctEditController"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getProfSubAcctOptions : {
				url : 'do/cda/dataList/profSubAcctOptions',
				method : 'post'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.filterEventCodes = ctrl.dialogsModule.params.filterEventCodes || [];
		ctrl.serviceSubEvents = ctrl.dialogsModule.params.serviceSubEvents || [];
		ctrl.isEdit = ctrl.dialogsModule.params.isEdit;
		ctrl.profSubAcct = {};
		ctrl.formName = "profSubAcctForm";
		
		ctrl.init = function() {
			ctrl.initGeneral().then(function() {
				ctrl.profSubAcct = ctrl.dialogsModule.params.profSubAcct || {};
			});
		};
		
		ctrl.initGeneral = function() {
			var defer = $q.defer();
			
			getProfSubAcctOptions().then(function(response) {
				ctrl.yesNoOptions = response.yesNoOptions;
				ctrl.categoryOptions = response.categoryOptions;
				defer.resolve();
			}, angular.noop);
			
			var eventCodeData = [];
			for (var i = 0; i < ctrl.serviceSubEvents.length; i++) {
				var dataObj = {};
				dataObj.label = ctrl.serviceSubEvents[i].subeventName;
				dataObj.value = ctrl.serviceSubEvents[i].subeventCode;
				dataObj.extProperty = ctrl.serviceSubEvents[i];
				eventCodeData[eventCodeData.length] = dataObj;
			}
			ctrl.eventCodeOptions = eventCodeData;
			
			ctrl.acctRuleInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.acctRule.tableConfigs.table, ctrlAcctRuleRowSelectionChanged);
			ctrl.acctRuleInnerGridOptions.data = 'vm.profSubAcct.acctRules';
			
			return defer.promise;
		};
		function getProfSubAcctOptions() {
			var defer = $q.defer();
			ajaxService.getProfSubAcctOptions().then(function(response) {
				var profSubAcctOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
//						tempValue.data = value;
						tempValue = value;
						profSubAcctOptions[key] = tempValue;
					});
				}
				defer.resolve(profSubAcctOptions);
			});
			return defer.promise;
		}
		
		ctrl.eventCodeChanged = function(value) {
			if (ctrl.profSubAcct.subeventCode) {
				for (var i = 0; i < ctrl.eventCodeOptions.length; i++) {
					var _event = ctrl.eventCodeOptions[i];
					if (_event.value == ctrl.profSubAcct.subeventCode) {
						if (ctrl.isEdit != 'Y') {// add page
							//
							angular.forEach(ctrl.filterEventCodes, function(filterEventCode, index) {
								if (ctrl.profSubAcct.subeventCode == filterEventCode) {
									// changed eventCode has used
									ctrl.profSubAcct.subeventCode = '';
									modalService.openErrorDialogs({
										messages : [ 'js.info.profsubacct.duplicate-subeventcode' ]
									});
									return;
								}
							});
							// if subeventCode change, need empty value
							ctrl.profSubAcct.acctRules = [];
							ctrl.profSubAcct.drAcctCode = '';
							ctrl.profSubAcct.drAcctDesc = '';
							ctrl.profSubAcct.crAcctCode = '';
							ctrl.profSubAcct.crAcctDesc = '';
							ctrl.profSubAcct.acctRuleFlag = 'N';
						}
						//
						resetAcctByEventExtProperty(ctrl, _event);
						break;
					}
				}
			}
		};
		
		function resetAcctByEventExtProperty (ctrl , _event) {
			ctrl.profSubAcct.eventExtProperty = _event.extProperty;
			
			ctrl.profSubAcct.crAcctType = ctrl.profSubAcct.eventExtProperty.glCrAcctType;
			ctrl.profSubAcct.drAcctType = ctrl.profSubAcct.eventExtProperty.glDrAcctType;
			
			// for init change row.entity readonly or not
			if (ctrl.profSubAcct.acctRules) {
				angular.forEach(ctrl.profSubAcct.acctRules, function(acctRule, key) {
					acctRule.glDrAcctFlag = ctrl.profSubAcct.eventExtProperty.glDrAcctFlag;
					acctRule.glCrAcctFlag = ctrl.profSubAcct.eventExtProperty.glCrAcctFlag;
				});
			} 
		}
		
		function ctrlAcctRuleRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedAcctRules = selectedRows;
		}
		
		ctrl.addAcctRule = function() {
			if (!ctrl.profSubAcct.acctRules) {
				ctrl.profSubAcct.acctRules = [];
			}
			ctrl.profSubAcct.acctRules.push({
				// for add change row.entity readonly or not
				glDrAcctFlag : ctrl.profSubAcct.eventExtProperty.glDrAcctFlag,
				glCrAcctFlag : ctrl.profSubAcct.eventExtProperty.glCrAcctFlag
			});
		};

		ctrl.ok = function() {
			if ($scope.profSubAcctForm) {
				$scope.profSubAcctForm.$setSubmitted();
				if(!$scope.profSubAcctForm.$valid) {
					dialogsModule.showErrorMessages($scope.profSubAcctForm);
					return;
				}
			}
			if ((!$scope.profSubAcctForm || $scope.profSubAcctForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.profSubAcct) !== false)) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
